package web.servlet;

import base.Constants;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import pojo.Course;
import service.CourseService;
import service.imp.CourseServiceImpl;
import utils.DateUtils;
import utils.UUIDUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/courseSalesInfo")
public class CourseSalesInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            //创建Course对象
            Course course = new Course();

            //创建map集合，收集数据
            Map<String,Object> map = new HashMap<>();

            //创建磁盘工厂对象
            DiskFileItemFactory factory = new DiskFileItemFactory();
            //创建文件上传核心类独享
            ServletFileUpload fileUpload = new ServletFileUpload(factory);
            //解析request对象，获取表单项集合
            List<FileItem> list = fileUpload.parseRequest(req);
            //遍历集合，加以判断
            for (FileItem fileItem : list) {
                boolean formField = fileItem.isFormField();
                if (formField){
                    //普通表单项
                    String fieldName = fileItem.getFieldName();
                    String fieldValue = fileItem.getString("utf-8");
                    map.put(fieldName,fieldValue);
                }else {
                    //文件上传项
                    //获取文件名
                    String fieldName = fileItem.getName();
                    //拼接新的文件名 使用UUID保证不重复
                    String newFileName = UUIDUtils.getUUID() + "_" + fieldName;

                    //获取输入流
                    InputStream inputStream = fileItem.getInputStream();

                    //获取项目的运行目录
                    String realPath = this.getServletContext().getRealPath("/");
                    //截取路径
                    String webappsPath = realPath.substring(0, realPath.indexOf("lagou_edu_home"));
                    //拼接路径

                    //创建输出流
                    FileOutputStream fileOutputStream = new FileOutputStream(webappsPath+"/upload/"+newFileName);

                    //使用IOUtils实现文件的copy
                    IOUtils.copy(inputStream,fileOutputStream);

                    //关闭流
                    fileOutputStream.close();
                    inputStream.close();

                    //将图片路径进行保存
                    map.put("course_img_url", Constants.LOCAL_URL+"/upload/"+newFileName);
                }

            }

            //将map中数据封装到course独享
            BeanUtils.populate(course,map);

            CourseService courseService = new CourseServiceImpl();

            if (map.get("id") != null){
                //修改操作
                //补全数据
                String dateFormart = DateUtils.getDateFormart();
                course.setUpdate_time(dateFormart);

                //业务处理
                String result = courseService.updateCourseSalesInfo(course);

                //响应结果
                resp.getWriter().print(result);
            }else {
                //新建操作
                //补全数据
                String dateFormart = DateUtils.getDateFormart();
                course.setCreate_time(dateFormart);
                course.setUpdate_time(dateFormart);
                course.setStatus(1);

                //业务处理

                String result = courseService.saveCourseSalesInfo(course);


                //响应结果
                resp.getWriter().print(result);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
